class Solution(object):
    def minOperations(self, nums):
        n = len(nums)
        nums = set(nums)
        nums = list(nums)
        m = len(nums)
        nums.sort()
        max_ans = 0
        left = 0
        right = 0
        while right < m:
            while nums[right] - nums[left] > n - 1:
                left +=1
            max_ans = max(max_ans, right - left + 1)
            right += 1
        return n - max_ans

data = Solution()
nums = [8,5,9,9,8,4]
print(data.minOperations(nums))